@file:Suppress("UNCHECKED_CAST")

package com.first.nell.extend

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.first.nell.base.BaseActivity
import com.first.nell.enums.PageTagEnum
import com.first.nell.home.HomeActivity
import com.first.nell.info.InfoActivity
import com.first.nell.info.cotediva.WalletListActivity
import com.first.nell.info.face.FaceDetectActivity
import com.first.nell.info.idcard.IdentityCardActivity
import com.first.nell.launch.LoginInputMobileActivity
import com.first.nell.launch.LoginInputOtpActivity
import com.first.nell.loan.LoanConfirmActivity
import com.first.nell.loan.LoanLoaningActivity
import com.first.nell.loan.LoanRejectActivity
import com.first.nell.loan.LoanReviewActivity
import com.first.nell.repay.RepayActivity
import com.first.nell.utils.InfoProcessUtil
import com.first.nell.utils.UsageCountryUtil
import com.first.nell.utils.UserStateUtil

inline fun <reified T> AppCompatActivity.startFirstNellToFirstNellNextFirstNellActivity(params: HashMap<String, String> = HashMap()) {
    try {
        val intent = Intent(this, T::class.java)
        params.keys.forEach { gkey ->
            intent.putExtra(gkey, params[gkey])
        }
        startActivity(intent)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

inline fun <reified T> AppCompatActivity.startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish(
    params: HashMap<String, String> = HashMap()
) {
    try {
        val intent = Intent(this, T::class.java)
        params.keys.forEach { gkey ->
            intent.putExtra(gkey, params[gkey])
        }
        startActivity(intent)
        finish()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

inline fun <reified T> AppCompatActivity.startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop(
    params: HashMap<String, String> = HashMap()
) {
    try {
        val intent = Intent(this, T::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
        params.keys.forEach { gkey ->
            intent.putExtra(gkey, params[gkey])
        }
        startActivity(intent)
        finish()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

fun Fragment.startFirstNellToFirstNellNextFirstNellFromFirstNellPage(
    pageTag: PageTagEnum,
    params: HashMap<String, String> = HashMap()
) {
    (requireContext() as BaseActivity).startFirstNellToFirstNellNextFirstNellFromFirstNellPage(
        pageTag = pageTag,
        params = params
    )
}

fun AppCompatActivity.startFirstNellToFirstNellNextFirstNellFromFirstNellPage(
    pageTag: PageTagEnum,
    params: HashMap<String, String> = HashMap()
) {
    try {
        if (!UserStateUtil.isFirstNellDidFirstNellLogin()) {
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<LoginInputMobileActivity>(
                params
            )
            return
        }

        if (InfoProcessUtil.isFirstNellRejectFirstNellNow()) {
            if (this !is LoanRejectActivity) {
                startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<LoanRejectActivity>(
                    params
                )
            }
            return
        } else if (InfoProcessUtil.isFirstNellRepayFirstNellNow()) {
            if (this !is RepayActivity) {
                startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<RepayActivity>(
                    params
                )
            }
            return
        } else if (InfoProcessUtil.isFirstNellLoaningFirstNellNow()) {
            if (this !is LoanLoaningActivity) {
                startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<LoanLoaningActivity>(
                    params
                )
            }
            return
        } else if (InfoProcessUtil.isFirstNellReviewingFirstNellNow()) {
            if (this !is LoanReviewActivity) {
                startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<LoanReviewActivity>(
                    params
                )
            }
            return
        }

        if (pageTag == PageTagEnum.launch) {
            //判断权限
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(
                params
            )

        } else if (pageTag == PageTagEnum.permission) {
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(
                params
            )

        } else if (pageTag == PageTagEnum.login_mobile) {
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(
                params
            )

        } else if (pageTag == PageTagEnum.home) {
            if (InfoProcessUtil.isFirstNellFinishFirstNellAllFirstNellPageFirstNellConfig()) {
                startFirstNellToFirstNellNextFirstNellActivity<LoanConfirmActivity>()
            } else {
                val curStep = InfoProcessUtil.getFirstNellCurrentFirstNellStep()
                if (curStep?.contains("ocr_info", true) == true) {
                    startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<IdentityCardActivity>()
                } else if (curStep?.contains("selfie_invo", true) == true) {
                    startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<FaceDetectActivity>()
                } else {
                    if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() || UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                        if (curStep?.contains("account_info", true) == true) {
                            startFirstNellToFirstNellNextFirstNellActivity<WalletListActivity>()
                        } else {
                            startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                        }
                    } else {
                        startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                    }
                }
            }
        } else if (pageTag == PageTagEnum.info) {
            if (InfoProcessUtil.isFirstNellFinishFirstNellAllFirstNellPageFirstNellConfig()) {
                startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<LoanConfirmActivity>()
            } else {
                val curStep = InfoProcessUtil.getFirstNellCurrentFirstNellStep()
                if (curStep?.contains("ocr_info", true) == true) {
                    startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<IdentityCardActivity>()
                } else if (curStep?.contains("selfie_invo", true) == true) {
                    startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<FaceDetectActivity>()
                } else {
                    if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() || UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                        if (curStep?.contains("account_info", true) == true) {
                            startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<WalletListActivity>()
                        }
                    }
                }
            }
        } else if (pageTag == PageTagEnum.ocr) {
            if (InfoProcessUtil.isFirstNellFinishFirstNellAllFirstNellPageFirstNellConfig()) {
                startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<LoanConfirmActivity>()
            } else {
                val curStep = InfoProcessUtil.getFirstNellCurrentFirstNellStep()
                if (curStep?.contains("ocr_info", true) == true) {
                    //startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<IdentityCardActivity>()
                } else if (curStep?.contains("selfie_invo", true) == true) {
                    startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<FaceDetectActivity>()
                } else {
                    if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() || UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                        if (curStep?.contains("account_info", true) == true) {
                            startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<WalletListActivity>()
                        } else {
                            startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                        }
                    } else {
                        startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                    }
                }
            }
        } else if (pageTag == PageTagEnum.selfie) {
            if (InfoProcessUtil.isFirstNellFinishFirstNellAllFirstNellPageFirstNellConfig()) {
                startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<LoanConfirmActivity>()
            } else {
                val curStep = InfoProcessUtil.getFirstNellCurrentFirstNellStep()
                if (curStep?.contains("ocr_info", true) == true) {
                    startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<IdentityCardActivity>()
                } else if (curStep?.contains("selfie_invo", true) == true) {
//                    startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<FaceDetectActivity>()
                } else {
                    if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() || UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                        if (curStep?.contains("account_info", true) == true) {
                            startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<WalletListActivity>()
                        } else {
                            startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                        }
                    } else {
                        startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                    }
                }
            }
        } else if (pageTag == PageTagEnum.apply) {
            val curStepResult: String? =
                InfoProcessUtil.getFirstNellNextFirstNellPageFirstNellConfigFirstNellAfterFirstNellApplyFirstNellPage()
            if (curStepResult.isNullOrBlank()) {
                println("all steps finish")
                startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<LoanConfirmActivity>()
            } else {
                println("=====page.apply:$curStepResult")
                curStepResult.let { curStep ->
                    if (curStep.contains("ocr_info", true)) {
                        startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<IdentityCardActivity>()

                    } else if (curStep.contains("selfie_invo", true)) {
                        startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<FaceDetectActivity>()
                    } else {
                        if (UsageCountryUtil.isFirstNellCountryFirstNellCotediva() || UsageCountryUtil.isFirstNellCountryFirstNellXaf()) {
                            if (curStep.contains("account_info", true)) {
                                startFirstNellToFirstNellNextFirstNellActivityFirstNellAndFirstNellFinish<WalletListActivity>()
                            } else {
                                startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                            }
                        } else {
                            startFirstNellToFirstNellNextFirstNellActivity<InfoActivity>()
                        }
                    }
                }
            }
        } else if (pageTag == PageTagEnum.repay) {
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(
                params
            )
        } else if (pageTag == PageTagEnum.review) {
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(
                params
            )
        } else if (pageTag == PageTagEnum.loaning) {
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(
                params
            )
        } else if (pageTag == PageTagEnum.reject) {
            startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(
                params
            )
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

/** 启动登录页-输入手机号页 */
fun AppCompatActivity.startFirstNellToFirstNellLoginFirstNellActivity() {
    startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<LoginInputMobileActivity>()
}

/** 启动登录页-输入手机号页 */
fun Fragment.startFirstNellToFirstNellLoginFirstNellActivity() {
    (requireContext() as BaseActivity).startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<LoginInputMobileActivity>()
}

/** 启动登录页-输入验证码页 */
fun AppCompatActivity.startFirstNellToFirstNellOtpFirstNellActivity() {
    startFirstNellToFirstNellNextFirstNellActivity<LoginInputOtpActivity>()
}

/** 启动审核中页 */
fun AppCompatActivity.startFirstNellToFirstNellLoanFirstNellReviewFirstNellActivity() {
    startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<LoanReviewActivity>()
}
